package com.base.caselib;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.caselib.adapter.CaseListAdapter;
import com.base.caselib.bean.CaseListBean;
import com.base.caselib.databinding.ActivityCaseListBinding;
import com.base.caselib.presenter.CaseListPresenter;
import com.base.caselib.view.CaseListView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.recy.BasicRecyViewHolder;
import com.base.commonlib.recy.utils.SpaceItemDecoration;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/10/21.
 * 案件列表
 */
@Route(path = "/case/caselist")
public class CaeListActivity extends CBaseBarMvpActivity<CaseListPresenter> implements CaseListView, View.OnClickListener {
    private ActivityCaseListBinding mBinding;
    private SpaceItemDecoration mCustomDivider;
    private CaseListAdapter caseListAdapter;
    private CaseListBean mCaseListBean;
    ImageView imageView;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_case_list;
    }


    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityCaseListBinding) mRootBinding;
        imageView=(ImageView) findViewById(R.id.iv_search);
        setBackNavigation();
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        setBarTitle("案件列表");
    }

    @Override
    protected void start() {

        mPresenter = new CaseListPresenter(this, this);
        mPresenter.start();

        mBinding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.refreshLayout.setHeaderHeight(60);
        mBinding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        mBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.onLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.onRefresh();
            }
        });


        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        if (caseListAdapter == null) {
            caseListAdapter = new CaseListAdapter(R.layout.item_caselist, mContext);
            mCustomDivider = new SpaceItemDecoration();
        }

        caseListAdapter.setItemClickListener(new BasicRecyViewHolder.OnItemClickListener() {
            @Override
            public void OnItemClick(View v, int adapterPosition) {
                ARouter.getInstance().build("/case/casedetail")
//                        .withString("cformId","YBPo9kX2oMP5fQABGjy")
//                        .withString("id","RgptqbRqj4AVKUuWA1V")
                        .withString("cformId",mCaseListBean.getRows().get(adapterPosition).getCformId())
                        .withString("id", mCaseListBean.getRows().get(adapterPosition).getId())
                        .navigation();
            }
        });

        mBinding.cyCaselist.setLayoutManager(manager);
        mBinding.cyCaselist.setAdapter(caseListAdapter);
        mBinding.cyCaselist.addItemDecoration(mCustomDivider);
        mPresenter.getCaseList();
    }


    @Override
    public void returnCaseListBean(CaseListBean bean) {
        mCaseListBean = bean;
        caseListAdapter.refreshDatas(bean.getRows());
        mBinding.refreshLayout.finishRefresh();
        mBinding.refreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter!=null){
            mPresenter.release();
        }
    }
}
